package afterVote;

import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.io.File;
import java.io.IOException;
import java.io.PrintStream;
import java.net.Socket;
import java.security.MessageDigest;

import javax.swing.JButton;

import org.silvertunnel.netlib.api.NetLayer;
import org.silvertunnel.netlib.api.NetSocket;

import protocol.Info;
import sun.misc.BASE64Encoder;
import tor.Client2;
import vote.VotePanelDescriptor;
import wizard.WizardPanelDescriptor;

public class AfterVotePanelDescriptor extends WizardPanelDescriptor implements ActionListener
{
    public static final String IDENTIFIER = "AFTER_VOTE_PANEL";
    private AfterVotePanel avp;
    private String voteToSend;

    public AfterVotePanelDescriptor()
    {
        avp = new AfterVotePanel();
        avp.addButtonsActionListener( this );
        setPanelDescriptorIdentifier( IDENTIFIER );
        setPanelComponent( avp );
    }

    public String getNextPanelDescriptor()
    {
        return null;
    }

    public String getBackPanelDescriptor()
    {
        return VotePanelDescriptor.IDENTIFIER;
    }

    public void aboutToDisplayPanel()
    {
        voteToSend = (String)getWizard().getPanelData( getBackPanelDescriptor() + "voteToSend" );
        try
        {
            MessageDigest md5 = MessageDigest.getInstance( "MD5" );
            BASE64Encoder encoder = new BASE64Encoder();
            String voteDigest = encoder.encode( md5.digest( voteToSend.getBytes() ) );
            avp.setInfoText( "Digest of your vote, copy it: " + voteDigest );
        }
        catch( Exception e )
        {
            e.printStackTrace();
        }
        getWizard().setNextFinishButtonEnabled( false );
    }

    int port = Info.TOR_PORT;
    String host = Info.HOST;
    boolean sent = false;

    @Override
    public void actionPerformed( ActionEvent arg0 )
    {
        try
        {
            if( ( (JButton)arg0.getSource() ).getText().equals( "Send using TOR network" ) )
            {
                System.out.println( "Start using TOR" );
                System.setErr( new PrintStream( new File( "client_output.txt" ) ) );
                Thread t = new Thread( new Runnable()
                {
                    public void run()
                    {
                        NetLayer nl = null;
                        try
                        {
                            Client2 c = new Client2();
                            // System.out.println( "PKT1" );
                            nl = c.makeNetLayer();
                            // System.out.println( "PKT2" );
                            NetSocket ns = c.connect( host, port, nl );
                            // System.out.println( "PKT3" );
                            c.write( ns, ( voteToSend + "\r\n" ) );
                            sent = true;
                            // System.out.println( "PKT4" );
                            Thread.sleep( 3000 );
                            ns.close();
                            // String nowa = c.read( ns );
                            // System.out.println( "PKT5" );
                            // System.out.println( nowa );
                            // System.out.println( "PKT6" );
                            // c.disconnect( ns, nl );
                        }
                        catch( Exception e )
                        {
                            try
                            {
                                nl.clear();
                            }
                            catch( IOException e1 )
                            {
                            }
                            throw new RuntimeException();
                        }
                    }
                } );
                t.start();
                Thread.sleep( 20000 );
                try
                {
                    t.stop();
                    t.destroy();
                }
                catch( Exception e )
                {
                }
                if( sent == false )
                {
                    avp.setInfoText( "Vote has been sent by TOR!!!!!!!!!!" );
                    System.out.println( "VOTE HAS BEEN SENT BY TOR" );
                }
                else
                {
                    avp.setInfoText( "Transmission error, please resend!!!!!!!!!!" );
                    System.out.println( "VOTE SENDING ERROR" );
                }
            }
            else
            {
                System.out.println( "Try send normal" );
                Socket socket = new Socket( Info.HOSTIN, port );
                socket.getOutputStream().write( ( voteToSend + "\r\n" ).getBytes() );
                socket.close();
                avp.setInfoText( "Vote has been sent!!!!!!!!!!" );
                System.out.println( "VOTE SENT" );
            }
        }
        catch( Exception e )
        {
        }
    }
}
